package com.gitee.srurl.core.util.valid;

import cn.hutool.extra.validation.ValidationUtil;
import com.gitee.srurl.core.enums.ResponseCodeEnums;
import com.gitee.srurl.core.exception.ServiceException;
import jakarta.validation.ConstraintViolation;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Set;

/**
 * @author zxw
 * @date 2023/1/15 下午8:58
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class ValidUtils {

    public static void validate(Object object, Class<?>... groups) {

        Set<ConstraintViolation<Object>> constraintViolations = ValidationUtil.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
            throw new ServiceException(constraint.getMessage(), ResponseCodeEnums.VALIDATE_FAIL.code());
        }
    }
}
